/*
 * Copyright @ 2010 Radomir Mladenovic and BugsIO Solutions
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 *       
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.gjutil.caching.provider;

//import java.util.logging.Logger;

import com.googlecode.gjutil.caching.CachedItemHolder;

public abstract class SimpleCacheBasedProvider extends AbstractCacheProvider {
	//private static final Logger log = Logger.getLogger(SimpleCacheBasedProvider.class.getName());

	public synchronized Object getFromCache(Object key) {		
		SimpleCache cache = getCache();
		//log.warning("Get " + key + " from " + cache);
		if(cache == null)
			return null;
		CachedItemHolder itemHolder = cache.get(key);
		//log.warning("Get " + key + " item " + itemHolder);
		if(itemHolder != null)
			return itemHolder.getValue();
		return null;
	}

	public synchronized void putInCache(Object key, Object value, int expirationSeconds, Boolean setAlways) {
		SimpleCache cache = getCache();
		//log.warning("Put " + key + " to " + cache);
		if(cache == null)
			return;
		if (value == null && !isCacheNull())
			return;
		int exp = expirationSeconds > 0 ? expirationSeconds : getExpirationSeconds();
		cache.put(key, value, exp);
	}

	/**
	 * Returns cache instance for the current context.
	 */
	protected abstract SimpleCache getCache();
}
